#### TABLE D.1: DEMAND EFFECTS
#### Extrinsic motivation interaction analysis

rm(list = ls())
source("./2_code/00_setup.R")

#### LOAD AND PREPARE DATA ####

data3 <- fread(paste0(data_path, "data_study3.csv"), header = TRUE)

# Recode extrinsic motivation
data3 <- data3 %>%
  mutate(extrinsic_motivation = recode(extrinsic_motivation,
                                       "Totalmente en desacuerdo" = 1,
                                       "En desacuerdo" = 2,
                                       "Algo en desacuerdo" = 3,
                                       "Ni en desacuerdo, ni de acuerdo" = 4,
                                       "Algo de acuerdo" = 5,
                                       "De acuerdo" = 6,
                                       "Totalmente de acuerdo" = 7))


#### REGRESSIONS ####

entry_economic_em <- lm_robust(exclusion_s ~ treat_economic * extrinsic_motivation, 
                               data = data3[(treat=='economic' | treat=='control')])
entry_humanitarian_em <- lm_robust(exclusion_s ~ treat_humanitarian * extrinsic_motivation, 
                                   data = data3[(treat=='humanitarian' | treat=='control')])
prejudice_economic_em <- lm_robust(prejudice_s ~ treat_economic * extrinsic_motivation, 
                                   data = data3[(treat=='economic' | treat=='control')])
prejudice_humanitarian_em <- lm_robust(prejudice_s ~ treat_humanitarian * extrinsic_motivation, 
                                       data = data3[(treat=='humanitarian' | treat=='control')])


#### EXPORT TO LATEX ####

models <- list(entry_economic_em, entry_humanitarian_em, prejudice_economic_em, prejudice_humanitarian_em)

labels <- c(
  "treat_economic" = "Economic",
  "extrinsic_motivation" = "Extrinsic Motivation",
  "treat_economic:extrinsic_motivation" = "Economic x Extrinsic Motivation",
  "treat_humanitarian" = "Humanitarian",
  "treat_humanitarian:extrinsic_motivation" = "Humanitarian x Extrinsic Motivation"
)

latex_output <- as.character(
  modelsummary(
    models,
    output = "latex",
    coef_map = labels,
    gof_omit = "AIC|BIC|RMSE|R2|Adj.R2",
    title = "",
    stars = c("***" = 0.01, "**" = 0.05, "*" = 0.1),
    note = NULL
  )
)

writeLines(latex_output, paste0(tables_path, "table_D1.tex"))